package com.infitecs.eyas.quality.service.Imp;

import com.infitecs.eyas.boiler.dao.EfficiencyDao;
import com.infitecs.eyas.boiler.model.Efficiency;
import com.infitecs.eyas.boiler.service.EfficiencyService;
import com.infitecs.eyas.combustion.dao.BurnDao;
import com.infitecs.eyas.combustion.dao.ChimneyDao;
import com.infitecs.eyas.combustion.dao.CleanDao;
import com.infitecs.eyas.combustion.model.CalculateBurn;
import com.infitecs.eyas.combustion.model.CalculateChimney;
import com.infitecs.eyas.combustion.model.CalculateClean;
import com.infitecs.eyas.combustion.service.BurnService;
import com.infitecs.eyas.combustion.service.ChimneyService;
import com.infitecs.eyas.combustion.service.CleanService;
import com.infitecs.eyas.constant.Constant;
import com.infitecs.eyas.expression.dao.ExpressionDao;
import com.infitecs.eyas.expression.model.Formula;
import com.infitecs.eyas.mill.dao.MillSelectDao;
import com.infitecs.eyas.mill.dao.PipeCalculateDao;
import com.infitecs.eyas.mill.model.MillSelect;
import com.infitecs.eyas.mill.model.PipeCalculate;
import com.infitecs.eyas.mill.service.MillSelectService;
import com.infitecs.eyas.mill.service.PipeCalculateService;
import com.infitecs.eyas.power.dao.OperateFieldDao;
import com.infitecs.eyas.power.model.OperateField;
import com.infitecs.eyas.quality.dao.AshDao;
import com.infitecs.eyas.quality.dao.BurstDao;
import com.infitecs.eyas.quality.dao.ElementDao;
import com.infitecs.eyas.quality.dao.IndustryDao;
import com.infitecs.eyas.quality.model.Ash;
import com.infitecs.eyas.quality.model.Burst;
import com.infitecs.eyas.quality.model.Element;
import com.infitecs.eyas.quality.model.Industry;
import com.infitecs.eyas.quality.service.AshService;
import com.infitecs.eyas.quality.service.BurstService;
import com.infitecs.eyas.quality.service.ElementService;
import com.infitecs.eyas.runcoalbin.dao.CoalBunkerDao;
import com.infitecs.eyas.runcoalbin.model.CoalBunker;
import com.infitecs.eyas.runcoalbin.service.CoalBunkerService;
import com.infitecs.eyas.util.DoubleUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.xidea.el.Expression;
import org.xidea.el.impl.ExpressionImpl;

import java.util.HashMap;

@Service("elementService")
@Transactional
public class ElementServiceImp implements ElementService {
    @Autowired
    private ElementDao elementDao;
    @Autowired
    private ExpressionDao expressionDao;
    @Autowired
    private IndustryDao industryDao;
    @Autowired
    private OperateFieldDao operateFieldDao;
    @Autowired
    private AshDao ashDao;
    @Autowired
    private AshService ashService;
    @Autowired
    private BurstService burstService;
    @Autowired
    private BurstDao burstDao;
    @Autowired
    private EfficiencyDao efficiencyDao;
    @Autowired
    private EfficiencyService efficiencyService;
    @Autowired
    private CoalBunkerDao coalBunkerDao;
    @Autowired
    private CoalBunkerService coalBunkerService;
    @Autowired
    private BurnService burnService;
    @Autowired
    private BurnDao burnDao;
    @Autowired
    private CleanService cleanService;
    @Autowired
    private CleanDao cleanDao;
    @Autowired
    private ChimneyDao chimneyDao;
    @Autowired
    private ChimneyService chimneyService;
    @Autowired
    private MillSelectDao millSelectDao;
    @Autowired
    private MillSelectService millSelectService;
    @Autowired
    private PipeCalculateService pipeCalculateService;
    @Autowired
    private PipeCalculateDao pipeCalculateDao;
    @Override
    public double calculateQnetArByFormula1(int caseId) {
        Industry industry = industryDao.getIndustryByPowerCaseId(caseId);
        Element element = elementDao.getElementByPowerCaseId(caseId);
        Formula formula1 = expressionDao.getFormulaByName(Constant.FORMULA_NAME_1);
        Expression expression1 = new ExpressionImpl(formula1.getFormulaDescription());
        HashMap<String, Double> params = new HashMap<>();
        params.put("E3", industry.getMar());
        params.put("E26", element.getCar());
        params.put("E27", element.getHar());
        params.put("E28", element.getOar());
        params.put("E30", element.getSar());
        double qnetAr = DoubleUtil.getInstance().get2Double(Double.valueOf(expression1.evaluate(params).toString()));
        elementDao.updateQnetAr(qnetAr, caseId);
        return qnetAr;
    }

    @Override
    public double calculateQnetArOtherByFormula2(int caseId) {
        Element element = elementDao.getElementByPowerCaseId(caseId);
        Formula formula2 = expressionDao.getFormulaByName(Constant.FORMULA_NAME_2);
        Expression expression2 = new ExpressionImpl(formula2.getFormulaDescription());
        HashMap<String, Double> params = new HashMap<>();
        params.put("E15", element.getQnetAr());
        Double qnetArOther = DoubleUtil.getInstance().get2Double(Double.valueOf(expression2.evaluate(params).toString()));
        elementDao.updateQnetArOther(qnetArOther, caseId);
        return qnetArOther;
    }

    @Override
    public double calculateQgrArByFormula3(int caseId) {
        Industry industry = industryDao.getIndustryByPowerCaseId(caseId);
        Element element = elementDao.getElementByPowerCaseId(caseId);
        Formula formula3 = expressionDao.getFormulaByName(Constant.FORMULA_NAME_3);
        Expression expression3 = new ExpressionImpl(formula3.getFormulaDescription());
        HashMap<String, Double> params = new HashMap<>();
        params.put("E3", industry.getMar());
        params.put("E15", element.getQnetAr());
        params.put("E27", element.getHar());
        double qgrAr = DoubleUtil.getInstance().get2Double(Double.valueOf(expression3.evaluate(params).toString()));
        elementDao.updateQgrAr(qgrAr, caseId);
        return qgrAr;
    }

    @Override
    public double calculateQgrArOtherByFormula4(int caseId) {
        Element element = elementDao.getElementByPowerCaseId(caseId);
        Formula formula4 = expressionDao.getFormulaByName(Constant.FORMULA_NAME_4);
        Expression expression4 = new ExpressionImpl(formula4.getFormulaDescription());
        HashMap<String, Double> params = new HashMap<>();
        params.put("E19", element.getQgrAr());
        double qgrArOther = DoubleUtil.getInstance().get2Double(Double.valueOf(expression4.evaluate(params).toString()));
        elementDao.updateQgrArOther(qgrArOther, caseId);
        return qgrArOther;
    }

    @Override
    public double calculateSumByFormula6(int caseId) {
        Industry industry = industryDao.getIndustryByPowerCaseId(caseId);
        Element element = elementDao.getElementByPowerCaseId(caseId);
        Formula formula6 = expressionDao.getFormulaByName(Constant.FORMULA_NAME_6);
        Expression expression1 = new ExpressionImpl(formula6.getFormulaDescription());
        HashMap<String, Double> params = new HashMap<>();
        params.put("E3", industry.getMar());
        params.put("E6", industry.getAar());
        params.put("E26", element.getCar());
        params.put("E27", element.getHar());
        params.put("E28", element.getOar());
        params.put("E29", element.getNar());
        params.put("E30", element.getSar());
        double sum = DoubleUtil.getInstance().get2Double(Double.valueOf(expression1.evaluate(params).toString()));
        elementDao.updateSum(sum, caseId);
        return sum;
    }

    @Override
    public void updateCar(double car, int caseId) {
        car = DoubleUtil.getInstance().get2Double(car);
        elementDao.updateCar(car, caseId);
    }

    @Override
    public void updateHar(double har, int caseId) {
        har = DoubleUtil.getInstance().get2Double(har);
        elementDao.updateHar(har, caseId);
    }

    @Override
    public void updateOar(double oar, int caseId) {
        oar = DoubleUtil.getInstance().get2Double(oar);
        elementDao.updateOar(oar, caseId);
    }

    @Override
    public void updateNar(double nar, int caseId) {
        nar = DoubleUtil.getInstance().get2Double(nar);
        elementDao.updateNar(nar, caseId);
    }

    @Override
    public void updateSar(double sar, int caseId) {
        sar = DoubleUtil.getInstance().get2Double(sar);
        elementDao.updateSar(sar, caseId);
    }

    @Override
    public void updateQnetAr(double qnetAr, int caseId) {
        qnetAr = DoubleUtil.getInstance().get2Double(qnetAr);
        elementDao.updateQnetAr(qnetAr, caseId);
    }

    @Override
    public void updateQnetArOther(double qnetArOther, int caseId) {
        qnetArOther = DoubleUtil.getInstance().get2Double(qnetArOther);
        elementDao.updateQnetArOther(qnetArOther, caseId);
    }

    @Override
    public void updateQgrAr(double qgrAr, int caseId) {
        qgrAr = DoubleUtil.getInstance().get2Double(qgrAr);
        elementDao.updateQgrAr(qgrAr, caseId);
    }

    @Override
    public void updateQgrArOther(double qgrArOther, int caseId) {
        qgrArOther = DoubleUtil.getInstance().get2Double(qgrArOther);
        elementDao.updateQgrArOther(qgrArOther, caseId);
    }

    @Override
    public Element getElement(int caseId) {
        return elementDao.getElementByPowerCaseId(caseId);
    }

    @Override
    public void elementSave(Element element) {
        //重新计算元素分析
        elementSaveData(element);
        OperateField operateField = operateFieldDao.getOperateField(element.getPowerCaseId());
        //更新灰分分析
        Ash oldAsh = ashDao.getAshByPowerCaseId(element.getPowerCaseId());
        if (oldAsh == null) return;
        oldAsh.setOtherState(operateField.getOtherState());
        ashService.ashSaveData(oldAsh);
        //更新磨损腐蚀爆炸性
        Burst oldBurst = burstDao.getBurstByPowerCaseId(element.getPowerCaseId());
        if (oldBurst == null) return;
        burstService.boomSaveData(oldBurst);
        //更新锅炉效率及燃煤量
        Efficiency oldEfficiency = efficiencyDao.getEfficiencyByPowerCaseId(element.getPowerCaseId());
        if (oldEfficiency != null) {
            oldEfficiency.setE52State(operateField.getE52State());
            oldEfficiency.setE89State(operateField.getE89State());
            oldEfficiency.setE90State(operateField.getE90State());
            oldEfficiency.setE92State(operateField.getE92State());
            oldEfficiency.setE93State(operateField.getE93State());
            oldEfficiency.setE94State(operateField.getE94State());
            oldEfficiency.setE95State(operateField.getE95State());
            efficiencyService.efficiencySaveData(oldEfficiency);
        }
        //更新原煤仓
        CoalBunker oldCoalBunker = coalBunkerDao.getCoalBunker(element.getPowerCaseId());
        //更新原煤仓信息
        if (oldCoalBunker != null) {
            oldCoalBunker.setE9State(operateField.getE9State());
            coalBunkerService.designSaveData(oldCoalBunker);
        }
        //更新燃烧
        CalculateBurn calculateBurn = burnDao.getCalculateBurnByCaseId(element.getPowerCaseId());
        if (calculateBurn != null) {
            calculateBurn.setYyglState(operateField.getYyglState());
            calculateBurn.setE54State(operateField.getE54State());
            calculateBurn.setE88State(operateField.getE88State());
            calculateBurn.setE122State(operateField.getE122State());
            calculateBurn.setE155State(operateField.getE155State());
            calculateBurn.setE171State(operateField.getE171State());
            burnService.burnSaveData(calculateBurn);
        }
        //更新烟气净化系统
        CalculateClean calculateClean = cleanDao.getCalculateClean(element.getPowerCaseId());
        if (calculateClean != null) {
            calculateClean.setE221State(operateField.getE221State());
            calculateClean.setE222State(operateField.getE222State());
            calculateClean.setE231State(operateField.getE231State());
            calculateClean.setE241State(operateField.getE241State());
            calculateClean.setE242State(operateField.getE242State());
            calculateClean.setE248State(operateField.getE248State());
            calculateClean.setE258State(operateField.getE258State());
            cleanService.cleanDataSave(calculateClean);
        }
        //更新烟囱出口直径计算
        CalculateChimney calculateChimney = chimneyDao.getCalculateChimney(element.getPowerCaseId());
        if (calculateChimney != null) {
            calculateChimney.setE314State(operateField.getE314State());
            calculateChimney.setE315State(operateField.getE315State());
            calculateChimney.setE316State(operateField.getE316State());
            calculateChimney.setCaseId(operateField.getCaseId());
            chimneyService.chimneySaveData(calculateChimney);
        }

        //更新磨煤机选择
        MillSelect millSelect = millSelectDao.getMillSelect(element.getPowerCaseId());
        if (millSelect != null) {
            millSelect.setMyglState(operateField.getMyglState());
            millSelect.setE12State(operateField.getE12State());
            millSelect.setE74State(operateField.getE74State());
            millSelect.setE75State(operateField.getE75State());
            millSelect.setModelState(operateField.getModelState());
            millSelectService.chooseDataSave(millSelect);
        }
        //更新煤粉管道计算
        PipeCalculate pipeCalculate = pipeCalculateDao.getPipeCalculate(element.getPowerCaseId());
        if (pipeCalculate != null) {
            pipeCalculateService.pipelineDataSave(pipeCalculate);
        }
    }

    @Override
    public void elementSaveData(Element element) {
        //获得合计
        double sum = calculateSumByFormula6(element);
        element.setSum(sum);
        //收到基低位发热量Qnet.ar
        if (element.getQnetArState().equals("0")) {
            double qnetAr = calculateQnetArByFormula1(element);
            element.setQnetAr(qnetAr);
        }
        //收到基低位发热量Qnet.ar,other
        if (element.getQnetArOtherState().equals("0")) {
            double qnetArOther = calculateQnetArOtherByFormula2(element);
            element.setQnetArOther(qnetArOther);
        }
        //收到基高位发热量Qgr.ar
        if (element.getQgrArState().equals("0")) {
            double qgrAr = calculateQgrArByFormula3(element);
            element.setQgrAr(qgrAr);
        }
        //收到基高位发热量Qgr.ar，other
        if (element.getQgrArOtherState().equals("0")) {
            double qgrArOther = calculateQgrArOtherByFormula4(element);
            element.setQgrArOther(qgrArOther);
        }
        Element oldElement = elementDao.getElementByPowerCaseId(element.getPowerCaseId());
        //新增或者编辑
        if (oldElement == null) {
            elementDao.addElement(element);
        } else {
            elementDao.updateElement(element);
        }
        //更新只读或者修改状态
        OperateField operateField = new OperateField();
        operateField.setQnetArState(element.getQnetArState());
        operateField.setQnetArOtherState(element.getQnetArOtherState());
        operateField.setQgrArState(element.getQgrArState());
        operateField.setQgrArOtherState(element.getQgrArOtherState());
        operateField.setCaseId(element.getPowerCaseId());
        operateFieldDao.updateOperateFieldElement(operateField);
    }


    public double calculateQnetArByFormula1(Element element) {
        Industry industry = industryDao.getIndustryByPowerCaseId(element.getPowerCaseId());
        Formula formula1 = expressionDao.getFormulaByName(Constant.FORMULA_NAME_1);
        Expression expression1 = new ExpressionImpl(formula1.getFormulaDescription());
        HashMap<String, Double> params = new HashMap<>();
        params.put("E3", industry.getMar());
        params.put("E26", element.getCar());
        params.put("E27", element.getHar());
        params.put("E28", element.getOar());
        params.put("E30", element.getSar());
        return DoubleUtil.getInstance().get2Double(Double.valueOf(expression1.evaluate(params).toString()));
    }

    public double calculateQnetArOtherByFormula2(Element element) {
        Formula formula2 = expressionDao.getFormulaByName(Constant.FORMULA_NAME_2);
        Expression expression2 = new ExpressionImpl(formula2.getFormulaDescription());
        HashMap<String, Double> params = new HashMap<>();
        params.put("E15", element.getQnetAr());
        return DoubleUtil.getInstance().get2Double(Double.valueOf(expression2.evaluate(params).toString()));
    }

    public double calculateSumByFormula6(Element element) {
        Industry industry = industryDao.getIndustryByPowerCaseId(element.getPowerCaseId());
        Formula formula6 = expressionDao.getFormulaByName(Constant.FORMULA_NAME_6);
        Expression expression1 = new ExpressionImpl(formula6.getFormulaDescription());
        HashMap<String, Double> params = new HashMap<>();
        params.put("E3", industry.getMar());
        params.put("E6", industry.getAar());
        params.put("E26", element.getCar());
        params.put("E27", element.getHar());
        params.put("E28", element.getOar());
        params.put("E29", element.getNar());
        params.put("E30", element.getSar());
        return DoubleUtil.getInstance().get2Double(Double.valueOf(expression1.evaluate(params).toString()));
    }

    public double calculateQgrArByFormula3(Element element) {
        Industry industry = industryDao.getIndustryByPowerCaseId(element.getPowerCaseId());
        Formula formula3 = expressionDao.getFormulaByName(Constant.FORMULA_NAME_3);
        Expression expression3 = new ExpressionImpl(formula3.getFormulaDescription());
        HashMap<String, Double> params = new HashMap<>();
        params.put("E3", industry.getMar());
        params.put("E15", element.getQnetAr());
        params.put("E27", element.getHar());
        return DoubleUtil.getInstance().get2Double(Double.valueOf(expression3.evaluate(params).toString()));
    }

    public double calculateQgrArOtherByFormula4(Element element) {
        Formula formula4 = expressionDao.getFormulaByName(Constant.FORMULA_NAME_4);
        Expression expression4 = new ExpressionImpl(formula4.getFormulaDescription());
        HashMap<String, Double> params = new HashMap<>();
        params.put("E19", element.getQgrAr());
        return DoubleUtil.getInstance().get2Double(Double.valueOf(expression4.evaluate(params).toString()));
    }
}
